﻿using System.Text.RegularExpressions;
using LY.Common;
using LY.DataAccess.Repositor;
using LY.DataAccess.Repository;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace LY.Business.ServiceImpl
{
    public class MemberService : IMemberService
    {
        MemberRepository _repo;
        AccountRepository _acc;
        UserAddressRepository _useraddressRepository;

        public MemberService()
        {
            _repo = new MemberRepository();
            _acc = new AccountRepository();
            _useraddressRepository = new UserAddressRepository();

        }
        //系统消息
        public PagedList<U_Information_EX> QuerySystemInformation(InformationSearch search, PageView view)
        {
            return _repo.QuerySystemInformation(search, view);
        }
        //查询会员列表
        public PagedList<Member_EX> QueryMemberList(MemberSearch search, PageView view)
        {
            return _repo.QueryMemberList(search, view);
        }
        public PagedList<Member_EX> QueryMemberListByUser(MemberSearch search, int userid, PageView view)
        {
            return _repo.QueryMemberListByUser(search, userid, view);
        }

        public PagedList<Member_EX> QueryMemberListByDept(MemberSearch search, int deptid, PageView view)
        {
            return _repo.QueryMemberListByDept(search, deptid, view);
        }

        public PagedList<U_Member> QueryAssignMemberList(MemberSearch search, int deptid, PageView view)
        {
            return _repo.QueryAssignMemberList(search, deptid, view);
        }

        public PagedList<U_Member> QueryDeptAssignMemberList(MemberSearch search, PageView view)
        {
            return _repo.QueryDeptAssignMemberList(search, view);
        }

        public PagedList<U_Member> QueryAssignDeptMemberList(MemberSearch search, PageView view)
        {
            return _repo.QueryAssignDeptMemberList(search, view);
        }

        public int MailVerification(string m, int? id)
        {
            return _repo.MailVerification(m, id);
        }

        public int CardVerification(string name, int? IDType, string IDCode, int? id)
        {
            return _repo.CardVerification(name, IDType, IDCode, id);
        }

        public int Account_BankVerification(int accountid, string Bank, string Branch, string BankCardNo, string RealNameOfBank)
        {
            return _repo.Account_BankVerification(accountid, Bank, Branch, BankCardNo, RealNameOfBank);
        }
        public int Account_BankVerif(U_BankCard bank)
        {
            return (int)_repo.Insert(bank);
        }
        public int BankValidate(int? id)
        {
            return _repo.BankValidate(id);
        }

        public int SelAccountID(int? id)
        {
            return _repo.SelAccountID(id);
        }
        public List<Member_EX> GetMemberByName(string membername)
        {
            membername = Utility.ClearSafeStringParma(membername);
            return _repo.GetMemberName(membername);
        }
        public int SaveAuthBanks(U_BankCard bank, string payPassword, int? memberid)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                //银行卡
                _repo.Insert(bank);
                //支付密码;
                _acc.UpdatePaypwd(memberid, payPassword);
                scope.Complete();
                return 1;
            }
        }
        public int SaveAuthBank(U_BankCard bank, string payPassword)
        {
            return 1;

        }
        /// <summary>
        /// 
        /// 保存会员信息
        /// </summary>
        /// <param name="inves"></param>
        /// <returns></returns>
        public int SaveMember(U_Member member)
        {
            if (member.ID <= 0)
            {
                //新增
                return (int)_repo.Insert(member);
            }
            else//编辑
            {
                //由于数据库字段设计不能为null,所以设几个默认值
                //member.Birthday = member.Birthday == null ? new DateTime(1905,06,13) : member.Birthday;
                member.Email = member.Email == null ? "" : member.Email;
                member.Address = member.Address == null ? "" : member.Address;
                member.Mobilenumber = member.Mobilenumber == null ? "" : member.Mobilenumber;
                member.RealName = member.RealName == null ? "" : member.RealName;
                member.HeadImage = member.HeadImage == null ? "" : member.HeadImage;
                //这里暂时是只要手机号码有值，邮箱有值就把两个标志位就设置成true
                //if (member.Mobilenumber != "")
                //{
                //    member.IsPhoneValidate = true;
                //}
                //if (member.Email != "")
                //{
                //    member.IsEmailValidate = true;
                //}
                return (int)_repo.Update(member);
            }
        }
        /// <summary>
        /// 删除会员信息
        /// </summary> 
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteMember(int id)
        {
            return _repo.DeleteMember(id);
        }


        /// <summary>
        /// 
        /// 分配会员部门
        /// </summary>
        /// <param name="mids"></param>
        /// <param name="did"></param>
        /// <returns></returns>
        public int AssignDept(string mids, string did)
        {
            return _repo.AssignDept(mids, did);
        }

        /// <summary>
        /// 
        /// 分配会员到员工
        /// </summary>
        /// <param name="mids"></param>
        /// <param name="did"></param>
        /// <returns></returns>
        public int AssignUser(string mids, string uid)
        {
            return _repo.AssignUser(mids, uid);
        }

        /// <summary>
        /// 分配会员客服
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int AssignMember(string ids, string mid)
        {
            return _repo.AssignMember(ids, mid);
        }
        public int AccountVerification(string AccountName, int? id)
        {
            return _repo.AccountVerification(AccountName, id);
        }

        /// <summary>
        /// 检索会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Member_EX GetMember(int? id)
        {
            return _repo.GetMember(id);
        }

        //验证激活邮箱
        public int UpdEmailValidate(string membername, string email)
        {
            return _repo.SetEmailValidate(membername, email);
        }
        /// <summary>
        /// 保存安全问题
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SetQuestionOne(int id, string que, string an)
        {
            return _repo.SetQuestionOne(id, que, an);
        }
        public int SetQuestionTwo(int id, string que, string an)
        {
            return _repo.SetQuestionTwo(id, que, an);
        }
        public int SetQuestionThree(int id, string que, string an)
        {
            return _repo.SetQuestionThree(id, que, an);
        }

        //验证支付密码
        public int ValidatePayPass(string memberName, string accountName, string payPwd)
        {
            return _repo.ValidatePayPass(memberName, accountName, payPwd);
        }
        //修改登录密码
        public int UpdateLoginpwd(int? id, string password)
        {
            return _repo.UpdateLoginpwd(id, password);
        }
        //修改支付密码
        public int UpdatePaypwd(int? id, string password)
        {
            return _repo.UpdatePaypwd(id, password);
        }

        //我的会员
        public PagedList<U_Member> GetMyMemberList(MemberSearch search, PageView view)
        {
            return _repo.GetMyMemberList(search, view);
        }

        //会员好友
        public PagedList<U_Member> GetMyMemberFriendList(MemberSearch search, PageView view)
        {
            return _repo.GetMyMemberFriendList(search, view);
        }

        //会员好友添加
        public PagedList<U_Member> GetMyMemberFriendAddList(MemberSearch search, PageView view)
        {
            return _repo.GetMyMemberFriendAddList(search, view);
        }

        //会员好友添加
        public PagedList<U_Member> GetMyMemberFriendAcceptList(MemberSearch search, PageView view)
        {
            return _repo.GetMyMemberFriendAcceptList(search, view);
        }

        //会员好友添加
        public PagedList<U_Member> MyMemberFriendAccept(MemberSearch search, PageView view)
        {
            return _repo.GetMyMemberFriendAcceptList(search, view);
        }

        /// <summary>
        /// 添加会员好友
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int InsertMemberFriend(int applyfriendmemberid, int acceptfriendmemberid)
        {
            return _repo.InsertMemberFriend(applyfriendmemberid, acceptfriendmemberid);
        }

        /// <summary>
        /// 接受会员好友
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int AcceptMemberFriend(int applyfriendmemberid, int acceptfriendmemberid)
        {
            return _repo.AcceptMemberFriend(applyfriendmemberid, acceptfriendmemberid);
        }
        /// <summary>
        /// @查询显示我的会员列表Name
        /// </summary>
        /// <param name="membername"></param>
        /// <param name="mkey"></param>
        /// <returns></returns>
        public List<U_Member> GetMyMemeber(MemberSearch search)
        {
            return _repo.GetMyMemeber(search);
        }
        /// <summary>
        /// @查询显示我的好友列表Name
        /// </summary>
        /// <returns></returns>
        public List<U_MemberFriend_EX> GetMyFriends(MemberSearch search)
        {
            return _repo.GetMemberFriends(search);
        }
        /// <summary>
        /// 获得我的会员我的好友并集数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public List<U_Member> GetAllMemberandFriend(MemberSearch search)
        {
            return _repo.GetAllMemberandFriend(search);
        }
        /// <summary>
        /// 获得所有我的好友的MemberId
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public List<U_Member> GetMemberFriendsId(MemberSearch search)
        {
            return _repo.GetMemberFriendsId(search);
        }
        /// <summary>
        /// 获得所有我的会员MemeberId
        /// </summary>
        /// <param name="mkey"></param>
        /// <returns></returns>
        public List<U_Member> GetMyMemeberId(string mkey)
        {
            return _repo.GetMyMemberId(mkey);
        }
        /// <summary>
        /// 获得所有MemberId
        /// </summary>
        /// <param name="memberid"></param>
        /// <param name="mkey"></param>
        /// <returns></returns>
        public List<U_Member> GetAllId(string memberid, string mkey)
        {
            return _repo.GetAllId(memberid, mkey);
        }
        public List<U_Member> GetID()
        {
            return _repo.GetID();
        }
        //审核
        public int MessageAudit(int id, string audituser, int status, string datetime, string auditremark)
        {
            return _repo.MessageAudit(id, audituser, status, datetime, auditremark);

        }
        //个人积分
        public int GetMemberScore(int? id)
        {
            return _repo.GetMemberScore(id);
        }

        //查询今天注册的上一个会员编号
        public Member_EX SelectLastMemberToday(string date)
        {
            return _repo.SelectLastMemberToday(date);
        }

        //查询所有的会员统计记录
        public PagedList<U_MemberStatistics_EX> QueryMemberStatisticsList(MemberStatisticsSearch search, PageView view)
        {
            return _repo.QueryMemberStatisticsList(search, view);
        }

        //查询身份证，手机号码，邮箱是否重复
        public string ValidateAll(string idCode, string mobilePhone, string email, int id)
        {
            string result = "";
            int a = _repo.ValicateIDCodeRepeat(idCode, id);
            if (a > 0)
            {
                result = result + "身份证重复";
            }
            int b = _repo.ValidateMobilePhoneRepeat(mobilePhone, id);
            if (b > 0)
            {
                result = result + ",手机号码重复";
            }
            int c = _repo.ValidateEmailRepeat(email, id);
            if (c > 0)
            {
                result = result + ",邮箱重复";
            }
            return result;
        }

        //复杂的登陆
        public U_Member login(string userName, string userPwd)
        {
            //这里就要判断是，邮箱，手机号码，还是用户名登陆
            Regex em = new Regex("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
            Regex ph = new Regex("^[1][3578]\\d{9}$");
            Match matchem = em.Match(userName);
            Match matchph = ph.Match(userName);
            if (matchem.Success)//邮箱登录
            {
                return _repo.LoginByEmail(userName, userPwd);
            }
            else if (matchph.Success)//手机登陆
            {
                return _repo.LoginByPhone(userName, userPwd);
            }
            else //最后就是用户名登录
            {
                return _repo.login(userName, userPwd);
            }
        }

        //查询用户名是否已存在
        public string CheckUserName(string userName)
        {
            return _repo.CheckUserName(userName);
        }


        //查询该会员是否已经绑定了这张银行卡
        public string CheckBankCardDangDing(int memberId, string cardNo)
        {
            int result = _repo.CheckBankCardDangDing(memberId, cardNo);
            if (result >= 1)
            {
                return "yes";
            }
            else
            {
                return "no";
            }
        }

        //修改会员状态
        //public int updateMemberStatus(int? id, int status)
        //{
        //    return _repo.updateMemberStatus(id,status);
        //}


        public U_Member getMemberInfo(int? id)
        {
            return _repo.getMemberInfo(id);

        }
        //根据用户名、邮箱、手机号、查询用户信息（忘记密码）
        public U_Member ForgetPwd(string info)
        {
            return _repo.ForgetPwd(info);

        }

        //根据邮箱查询邮箱验证状态
        public U_Member getMemberStatus(string email)
        {
            return _repo.getMemberStatus(email);
        }

        #region 我的收货地址
        /// <summary>
        /// 地址列表
        /// </summary>
        /// <param name="MemberId">会员ID</param>
        /// <returns></returns>
        public List<U_UserAddress> GetAllUserAddress(int MemberId)
        {
            return _useraddressRepository.GetAllUserAddress(MemberId);
        }
        //地址列表
        public PagedList<U_UserAddress> GetUserAddressList(UserAddressSearch search, PageView view)
        {
            return _useraddressRepository.GetUserAddressList(search.MemberId, view);
        }

        /// <summary>
        /// 保存地址
        /// </summary>
        /// <param name="model">地址实体</param>
        /// <returns></returns>
        public int SaveUserAddress(U_UserAddress model)
        {
            return _useraddressRepository.SaveUserAddress(model);
        }
        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="Id">主键ID</param>
        /// <returns></returns>
        public int DeleteUserAddress(int Id)
        {
            return _useraddressRepository.DeleteUserAddress(Id);
        }
        /// <summary>
        /// 设置默认收货地址
        /// </summary>
        /// <param name="Id">主键ID</param>
        /// <returns></returns>
        public int SetDefaultAddress(int Id)
        {
            return _useraddressRepository.SetDefaultAddress(Id);
        }
        //修改默认地址
        public int UpdateDefaultAddress(int? memberid)
        {
            return _useraddressRepository.UpdateDefaultAddress(memberid);
        }
        /// <summary>
        /// 根据ID查询认收货地址
        /// </summary>
        /// <param name="Id">主键ID</param>
        /// <returns></returns>
        public U_UserAddress GetAddress(int Id)
        {
            return _useraddressRepository.GetAddress(Id);
        }
        #endregion




        public bool ExistsPhone(string phpone)
        {
            return _repo.ExistsPhone(phpone);
        }



        public int PayValidate(int? id)
        {
            return _repo.PayValidate(id);
        }


        public int ValidatePhone(U_Member member)
        {
            return _repo.ValidatePhone(member);
        }


        U_Member IMemberService.GetModelByName(string name)
        {
            return _repo.GetModelByName(name);
        }

        public U_Member GetModelBySftUserName(string sftUserName)
        {
            return _repo.GetModelBySftUserName(sftUserName);
        }
        //根据公司ID查询项目发起人信息
        public U_CompanyAnnex_EX GetMemberInfo(int? cid)
        {
            return _repo.GetMemberInfo(cid);
        }

        //(确定会员名唯一)
        public U_Member GetMemberInfoByName(string name)
        {
            return _repo.GetMemberInfoByName(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="menOauth"></param>
        /// <returns></returns>
        public int SaveMemberOauth(U_Member_oauth menOauth)
        {
            return _repo.SaveMemberOauth(menOauth);
        }
        public U_Member_oauth OauthLogin(string oauth_name,string oauth_openid)
        {
            return _repo.OauthLogin(oauth_name,oauth_openid);
        } 
    }
}
